package com.cn.aam.checaiduo.utils;

import android.text.TextUtils;

import com.cn.aam.checaiduo.ui.me.adapter.CalenderAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * monker on 2017/5/26.
 */

public class CalendarUtil {

    private static final String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";
    private static final String FORMAT_TIME1 = "yyyy-MM-dd HH:mm";
    private static final String FORMAT_TIME2 = "yyyy-MM-dd";
    private static final String FORMAT_TIME6 = "yyyy-MM";

    private static final String FORMAT_TIME3 = "yyyy年MM月";
    private static final String FORMAT_TIME4 = "yyyy";

    private static final String FORMAT_TIME5 = "yyyy年MM月dd日";

    /**
     * 时间：2017/5/31
     * 描述：获取当前时间
     */
    public static String getDateString() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        if (Integer.parseInt(mDay) > MaxDayOfMonth()) {
            mDay = String.valueOf(MaxDayOfMonth());
        }
        return mYear + "-" + (mMonth.length() == 1 ? "0" + mMonth : mMonth) + "-" + (mDay.length() == 1 ? "0" + mDay : mDay);
    }

    /**
     * 得到当年当月的最大日期
     **/
    public static int MaxDayOfMonth() {
        int curYear = Calendar.getInstance().get(Calendar.YEAR);
        int curMonth = Calendar.getInstance().get(Calendar.MONTH);

        Calendar time = Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR, curYear);
        time.set(Calendar.MONTH, curMonth);//注意,Calendar对象默认一月为0
        int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数
        return day;
    }

    /**
     * 根据当前日期获得是星期几
     *
     * @return
     */
    public static String getWeek(String time) {
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            Week += "周天";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            Week += "周一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
            Week += "周二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
            Week += "周三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
            Week += "周四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
            Week += "周五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            Week += "周六";
        }
        return Week;
    }

    /**
     * 时间：2017/5/31
     * 描述：获取这个月的天数
     */
    public static List<CalenderAdapter.DateItemB> getThisMonthDays() {
        List<CalenderAdapter.DateItemB> dateItemBs = new ArrayList<>();

        String curYear = Calendar.getInstance().get(Calendar.YEAR) + "";
        String curMonth = (Calendar.getInstance().get(Calendar.MONTH) + 1) + "";
        String today = Calendar.getInstance().get(Calendar.DAY_OF_MONTH) + "";
        int maxDayOfThisMonth = MaxDayOfMonth();
        String date = "";

        CalenderAdapter.DateItemB dateItemB = null;
        for (int i = 1; i <= maxDayOfThisMonth; i++) {
            date = curYear + "-" + curMonth + "-" + i;
            dateItemB = new CalenderAdapter.DateItemB();
            String week = getWeek(date);
            if (i < 10) {
                dateItemB.setDay("0" + i);
            } else {
                dateItemB.setDay(String.valueOf(i));
            }
            if (curMonth.length() < 2) {
                dateItemB.setDetailDate(curYear + "-0" + curMonth + "-" + dateItemB.getDay());
            } else {
                dateItemB.setDetailDate(curYear + "-" + curMonth + "-" + dateItemB.getDay());
            }
            dateItemB.setWeek(week);

            if (TextUtils.equals(today, i + "")) {
                dateItemB.setToday(true);
            }


            dateItemBs.add(dateItemB);
        }
        return dateItemBs;
    }


    /**
     * 时间：2017/5/31
     * 描述：某一天到到今天相隔多久
     */
    public static String apartTimeOfToday(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME);
        Date date = null;
        long dateTimeMillis = 0;
        try {
            date = format.parse(dateStr);
            dateTimeMillis = Math.abs(new Date().getTime() - date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String days = (dateTimeMillis / 1000 / 60 / 60 / 24) + "天";
        return days;
    }

    public static String timeFormat(long time) {
        Date d = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME2);
        return format.format(d);
    }


    /**
     * 时间：2017/5/31
     * 描述：今天
     */
    public static String getTodayTime() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME2);
        String time = format.format(date);
        return time;
    }

    /**
     * 时间：2017/5/31
     * 描述：一周前
     */
    public static String getWeekTime() {

        long sevenDays = 7l * 24 * 60 * 60 * 1000;

        Date date = new Date();
        long targetDay = date.getTime() - sevenDays;

        Date targetDate = new Date(targetDay);
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME2);
        String time = format.format(targetDate);
        return time;
    }

    /**
     * 时间：2017/5/31
     * 描述：一个月
     */
    public static String getMonthTime() {

        long month = 30l * 24 * 60 * 60 * 1000;//一个月

        Date date = new Date();
        long targetDay = date.getTime() - month;//一个月前

        Date targetDate = new Date(targetDay);
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME2);
        String time = format.format(targetDate);
        return time;
    }

    /**
     * 时间：2017/5/31
     * 描述：三个月
     */
    public static String getThreeMonthTime() {

        long threeMonth = 3l * 30 * 24 * 60 * 60 * 1000;

        Date date = new Date();
        long targetDay = date.getTime() - threeMonth;

        Date targetDate = new Date(targetDay);
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME2);
        String time = format.format(targetDate);
        return time;
    }

    /**
     * 时间：2017/5/31
     * 描述：半年
     */
    public static String getHarfYearTime() {

        long halfYear = 6l * 30 * 24 * 60 * 60 * 1000;

        Date date = new Date();
        long targetDay = date.getTime() - halfYear;

        Date targetDate = new Date(targetDay);
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME2);
        String time = format.format(targetDate);
        return time;
    }

    /**
     * 时间：2017/5/31
     * 描述：一个月
     */
    public static String getYearTime() {

        long year = 365l * 24 * 60 * 60 * 1000;

        Date date = new Date();
        long targetDay = date.getTime() - year;

        Date targetDate = new Date(targetDay);
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME2);
        String time = format.format(targetDate);
        return time;
    }

    /**
     * 时间：2017/6/5
     * 描述：是否到期
     */
    @Deprecated
    public static boolean isExpired(String dateStr) {

        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME);
        try {
            Date date = format.parse(dateStr);
            long targetTime = date.getTime();
            long newTime = new Date().getTime();
            if (newTime > targetTime) {
                return true;
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 时间：2017/6/7
     * 描述：年-月-日  日期
     */
    public static String getDateString(String stringDate) {
        if (TextUtils.isEmpty(stringDate)) return "";
        return stringDate.split(" ")[0];
    }


    /**
     * 时间：2017/6/20
     * 描述：2017-01-12 14:26-->2017年01月
     */
    public static String getYearAndMonth(String date) {

        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME1);
        Date time = null;
        try {
            time = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat format1 = new SimpleDateFormat(FORMAT_TIME3);
        return format1.format(time);
    }

    /**
     * 时间：2017/5/31
     * 描述：年份
     */
    public static String getYear(long date) {
        Date d = new Date(date);
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME4);
        return format.format(d);
    }

    /**
     * 时间：2017/6/29
     * 描述：2014-2-25-->2014年2月25日
     */
    public static String getDate5(String str) {
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME2);
        try {
            Date date = format.parse(str);
            format = new SimpleDateFormat(FORMAT_TIME5);
            String str1 = format.format(date);
            return str1;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * 时间：2017/6/30
     * 描述：当前月份
     */
    public static String getCurMonth() {
        int curYear = Calendar.getInstance().get(Calendar.YEAR);
        int curMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
        return curYear + "-" + curMonth + "-1";
    }


}